$(function () {
  let moreLimit = 0;
  let data = {
    pagenum: 1,
    pagesize: 8,
  };

  getList();
  $('.kr_body').on('click', '.kr_more', function () {
    data.pagesize += 8;
    $('.kr_more a').text('加载中......');
    getList();
  });

  function getList() {
    $.ajax({
      url: '/api/articles',
      data,
      success(res) {
        $('.kr_more a').text('更多');
        moreLimit = res.total == res.data.length;
        let str = template('news', res);
        $('.kr_body').html(str);
        if (moreLimit) {
          $('.kr_body').unbind('click');
          $('.kr_more a').css({
            color: '#ddd',
            borderColor: '#ddd',
          });
          $('.kr_more a').text('加载完成');
        }
      },
    });
  }
  let getZero = (num) => (num > 10 ? num : '0' + num);
  template.defaults.imports.Day = function (res) {
    let date = new Date(res);
    return getZero(date.getDate());
  };
  template.defaults.imports.Month = function (res) {
    let date = new Date(res);
    return getZero(date.getMonth() + 1);
  };
  template.defaults.imports.filterTime = function (res) {
    let date = new Date(res);
    let y = date.getFullYear();
    let m = getZero(date.getMonth() + 1);
    let d = getZero(date.getDate());
    let h = getZero(date.getHours());
    let mm = getZero(date.getMinutes());
    let s = getZero(date.getSeconds());
    return `${y}-${m}-${d} ${h}:${mm}:${s}`;
  };
});
